﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MaLu.Common;
using Wicresoft.Framework.Organization;
using System.Data;
using MaLu.Data;
using MaLu.BusiLogic;

namespace MaluJg.web.Visite
{
    public partial class VisiteList : VisitePage
    {
        public Guid DepartmentID
        {
            get
            {
                if (Request["DepartmentID"] != null)
                    return new Guid(Request["DepartmentID"]);
                if (ViewState["DepartmentID"] == null)
                    ViewState["DepartmentID"] = Guid.Empty;
                return (Guid)ViewState["DepartmentID"];
            }
            set
            {
                ViewState["DepartmentID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                Department mainD = OrganizationHelper.GetDepartmentInfo(CurrentUser.MainDepartmentID);
                string[] ids = mainD.IDPath.Split('/');
                if (ids.Length >= 4)
                    DepartmentID = new Guid(ids[3]);
                else
                    DepartmentID = new Guid(ids[ids.Length - 1]);
            }
            BindData();


        }

        private void BindData()
        {
            DataTable dtItem = SqlHelper.Instance.ExecuteDataTable(Provider, "select id ,Interviewee,DepartmentDisplayName,Contact,ArrangeStartTime ,ArrangeEndTime,replace( Leader,'''''','''') as Leader from Visite Order by CREATETIME desc");

            rptItem.DataSource = dtItem;
            rptItem.DataBind();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void rptItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            LinkButton lbtnDelete = e.Item.FindControl("lbtnDelete") as LinkButton;
            lbtnDelete.CommandArgument = (e.Item.DataItem as DataRowView).Row["ID"].ToString();
            lbtnDelete.Click += new EventHandler(lbtnDelete_Click);
        }

        void lbtnDelete_Click(object sender, EventArgs e)
        {
            Guid id = new Guid((sender as LinkButton).CommandArgument);

            if (BLVisite.DeleteVisite(Provider, id) == 0)
            {
                string script = "<script>alert('成功删除。');</script>";
                ClientScript.RegisterStartupScript(this.GetType(), "Error", script);
            }
            else
            {
                string script = "<script>alert('该项目已经填写了走村入户表项，无法删除。');</script>";
                ClientScript.RegisterStartupScript(this.GetType(), "Error", script);
            }
            BindData();
        }
    }
}
